<template>
  <div class="app-container">
    <!-- 搜索 -->
    <el-form ref="queryForm" :inline="true" v-show="showSearch">
      <el-form-item>
        <el-button type="primary" size="mini" @click="getAdd"
          >新建基准</el-button
        >
      </el-form-item>
      <el-dialog
        :titletype="titletype"
        :visible.sync="dialogVisibleOne"
        width="30%"
      >
        <el-form
          ref="addEditform"
          :rules="addEditformRules"
          :model="addEditform"
          label-width="100px"
        >
          <el-form-item label="基准指标:" prop="datum">
            <el-input v-model="addEditform.datum"></el-input>
          </el-form-item>
          <el-form-item label="基准名称：" prop="datumName">
            <el-input v-model="addEditform.datumName"></el-input>
          </el-form-item>
          <!-- <el-form-item label="报价基数" prop="scale">
            <el-input v-model="addEditform.scale"></el-input>
          </el-form-item> -->
          <el-form-item label="备注：">
            <el-input type="textarea" v-model="addEditform.remark"></el-input>
          </el-form-item>
        </el-form>
        <!-- v-if="titletype != '3'" -->
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisibleOne = false">取 消</el-button>
          <el-button type="primary" @click="addEditBtn">创建</el-button>
        </div>
        <!-- v-if="(titletype = '3')"s -->
        <!-- <div slot="footer" class="dialog-footer">
          <el-button @click="dialogVisibleOne = false">取消</el-button>
        </div> -->
      </el-dialog>
      <el-form-item prop="status">
        <el-input
          placeholder="请输入搜索关键字"
          v-model="page.datumName"
          clearable
        >
        </el-input>
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
        >
          搜索</el-button
        >
      </el-form-item>
      <el-form-item>
        <el-button @click="rcatform()"> 重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 表单 -->
    <el-table
      ref="singleTable"
      :data="tableData"
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column width="100" align="center" label="ID" prop="id" />
      <!-- <el-table-column
        width="150"
        align="center"
        prop="createTime"
        label="时间"
      >
        <template slot-scope="scope">
          <span>
            {{
              scope.row.createTime.slice(0, 10) +
              " " +
              scope.row.createTime.slice(11, 19)
            }}
          </span>
        </template>
      </el-table-column> -->
      <el-table-column width="150" align="center" prop="datum" label="基准比">
      </el-table-column>

      <el-table-column
        align="center"
        width="150"
        label="基准名称"
        prop="datumName"
      >
        <!-- <template slot-scope="scope">
          {{ scope.row.countCost.toFixed(2) }}
        </template> -->
      </el-table-column>
      <el-table-column
        align="center"
        width="150"
        label="状态"
        prop="countPrime"
      >
        <template scope="{row}">
          <el-switch
            v-model="row.flag"
            :active-color="activeblue"
            :inactive-color="activered"
            @change="editative(row)"
          >
          </el-switch>
        </template>
      </el-table-column>
      <el-table-column
        label="备注"
        align="center"
        width="200"
        prop="remark"
      ></el-table-column>

      <el-table-column align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button icon="el-icon-edit" @click="editBtn(scope.row)">
            编辑</el-button
          >
          <el-button
            icon="el-icon-delete-solid"
            @click="handleDelete(scope.row)"
            >删除</el-button
          >

          <el-button icon="el-icon-top" @click="expor(scope.row.id)"
            >用户分配</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- table -->

    <!-- 分页 -->
    <el-pagination
      @current-change="handleCurrentChange"
      @size-change="handleSizeChange"
      :page-sizes="[1, 2, 3, 4, 5, 10, 15]"
      :page-size="page.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>
  </div>
</template>

<script>
import {
  quoteList,
  adddatum,
  editdatum,
  deldatum,
} from "@/api/banch/banchmanrk";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
import storage from "@/utils/storage";
import { rowspan } from "@/utils/rowSpan";
export default {
  name: "Dept",
  components: { Treeselect },
  data() {
    return {
      //data
      addEditform: {
        id: "",
        datum: "",
        datumName: "",
        remark: "",
      },

      TotalPriceList: [],
      list: [],

      priviewList: [],

      quotoTitle: "",
      addEditformRules: {
        remark: [{ required: true, message: "请输入备注", trigger: "blur" }],
        datumName: [{ required: true, message: "请输入名称", trigger: "blur" }],
        datum: [
          {
            required: true,
            message: "指标保留2位小数",
            trigger: "blur",
            pattern: /^\d+.?\d{0,2}$/,
          },
        ],
      },
      totalPrice: 0,
      total: 0,
      page: {
        pageNum: 1,
        pageSize: 5,
        datumName: "",
      },
      dialogVisibleOne: false,
      dialogVisibleTwo: false,
      tableData: [],
      tableDataTwo: [],
      quoteTitle: "",
      // 显示搜索条件
      showSearch: true,

      // 弹出层标题
      titletype: "1",
      // 是否显示弹出层
      open: false,
      // 状态数据字典
      statusOptions: [],
      countPrimestatus: true,
      activeblue: "#13ce66",
      activered: "rgb(168,170,170)",

      // 查询参数
      queryParams: {
        deptName: undefined,
        status: undefined,
      },
      // 表单参数
      form: {},

      dialogFormVisible: false,

      formLabelWidth: "120px",
    };
  },
  created() {
    this.getList(this.page);
  },
  mounted() {},
  methods: {
    changeType(val) {
     
    },
    getTime() {
      let a = "2021-07-30T14:05:45.000+0800";
      let b = a.splice(0, 10);
     
    },
    getKeys() {
      return this.$uuid.v1();
    },
    //用户分配
    expor(id) {
    
      this.$router.push({ path: "/assigned", query: { id } });
    },
    // 新增修改
    addEditBtn() {
     
      if (this.titletype == "1") {
      
        this.$refs.addEditform.validate((valid) => {
          if (valid) {
            let { datum, remark, datumName } = this.addEditform;
            adddatum({ datum, remark, datumName }).then(
              (res) => {
                this.$message({
                  message: "恭喜你,创建成功",
                  type: "success",
                });
                this.dialogVisibleOne = false;
                this.getList(this.page);
                // let mess = res.data;
                // mess.quotoTitle = res.data.quoteTitle;
                // storage.quoteId = mess;
                // this.$router.push({
                //   path: "/region",
                // });
              },
              (err) => {
               
                this.$message({
                  message: "很遗憾，创建失败",
                  type: "success",
                });
              }
            );
          } else {
          
            return false;
          }
        });
      } else if (this.titletype == "2") {
        let { datum, datumName, remark, id } = this.addEditform;

        this.$refs.addEditform.validate((val) => {
          if (val) {
            editdatum({ datum, datumName, remark, id }).then(
              (res) => {
               
                this.$message({
                  message: "恭喜你，修改成功",
                  type: "success",
                });
                this.dialogVisibleOne = false;
                this.getList(this.page);
              },
              (err) => {
               
                this.$message({
                  message: "很遗憾，修改失败",
                  type: "error",
                });
              }
            );
          } else {
            return false;
          }
        });
      }
    },

    //开关
    editative(row) {
    
      // return;
      let { id, datum, datumName, flag, remark } = row;

      editdatum({ id, datum, datumName, status: flag ? 0 : 1, remark })
        .then((res) => {
          if (res.code == 200) {
            this.$message({
              message: res.msg,
              type: "success",
            });
          } else {
            this.$message({
              message: res.msg,
              type: "error",
            });
          }
        })
        .catch((e) => {
         
        })
        .finally((e) => {
          this.loading = false;
          // this.getList(this.page);
        });
    },

    handleCurrentChange(val) {
      this.page.pageNum = val;
      this.getList(this.page);
    },
    // 页数
    handleSizeChange(val) {
      this.page.pageSize = val;
      this.getList(this.page);
      // this.getList();
    },
    /** 查询列表 */
    getList(val) {
    
      this.loading = true;
      quoteList(val)
        .then((res) => {
        
          this.total = res.total;
          this.tableData = res.rows.map((res) => ({
            ...res,
            flag: res.status == 0 ? true : false,
          }));
        })
        .catch((e) => {
        
        })
        .finally((e) => {
          this.loading = false;
        });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置

    /** 搜索按钮操作 */
    handleQuery() {
    
      this.getList(this.page);
    },
    /** 重置按钮操作 */
    rcatform() {
      this.page.datumName = "";
      this.getList(this.page);
    },

    /** 新增按钮操作 */
    getAdd() {
      this.titletype = "1";
      this.dialogVisibleOne = true;
      this.addEditform.id = "";
      this.addEditform.datumName = "";
      this.addEditform.datum = "";
      this.addEditform.remark = "";
    },

    /** 编辑按钮操作 */
    editBtn(row) {
     
      this.titletype = "2";
      this.dialogVisibleOne = true;
      this.addEditform.id = row.id;
      this.addEditform.datum = row.datum;
      this.addEditform.datumName = row.datumName;
      this.addEditform.remark = row.remark;

      storage.quoteId = row;
      //   this.$router.push({
      //     path: "/region",
      //     // query:{
      //     //   id:row.id,
      //     //   quotoTitle:row.quotoTitle
      //     // }
      //   });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
     
      this.$confirm(
        '是否确认删除名称为"' + row.datumName + '"的数据项?',
        "警告",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      )
        .then(() => {
          deldatum(row.id).then(() => {
            this.getList();
            this.msgSuccess("删除成功");
          });
        })
        .catch(() => {});
    },
  },
};
</script>
<style lang="scss" scoped>
.el-pagination {
  margin-top: 30px;
}
.el-table-column {
  display: flex;
  justify-content: space-between;
}
.footer {
  width: 100%;
  height: 100px;
  display: flex;
  align-items: center;
  display: flex;
  justify-content: flex-end;
}
.title {
  line-height: 50px;
  font-size: 24px;
  font-weight: bold;
}
table {
  border-collapse: collapse;
  text-align: center;
  font-size: 12px;
}
td,
th {
  width: 150px;
  height: 30px;
  border-collapse: collapse;
  border: 1px solid grey;
}
.overCell {
  width: 250px;
  height: 70px !important;
  padding: 5px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: wrap;
}
</style>